package com.nbstar.film.spider.spider;

import com.nbstar.film.spider.http.SipderStringRequest;
import com.nbstar.film.spider.pojo.FilmInfo;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URLDecoder;
import java.net.URLEncoder;

@Slf4j
public class YouKuSpider extends BaseSpider {
    //private static Logger logger = LoggerFactory.getLogger(YouKuSpider.class);

    public YouKuSpider() {
        this.spiderName = "YOUKU";
        setRequest(new SipderStringRequest());
        getRequest().clearHeader();
        getRequest().addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0");
        getRequest().addHeader("Cookie", "__wpkreporterwid_=4447413d-a4d9-48a2-12d3-b8b9b04cbf1e; firstTimes=0; __ysuid=1567956544737wKV; cna=SAuVFGUKzzwCAW8K/FMuPP/w; juid=01dk8nsk9o2cgv; ysestep=4; yseidcount=6; ystep=9; isg=BBYWvDfQVf_xIGMwrqEaS42UZMwYt1rxL9U2VoB_P_mUQ7fd6ETCAfV131-vcFIJ; youku_history_word=[%22%25E4%25BA%25BA%25E7%2594%259F%25E4%25BD%2595%25E5%25A4%2584%25E4%25B8%258D%25E7%259B%25B8%25E9%2580%25A2%25E9%2599%2588%25E6%2585%25A7%25E5%25A8%25B4mv%22%2C%22%E8%89%BE%E4%BC%A6%22%2C%22%E4%B8%80%E4%BB%A3%E5%A5%B3%E7%9A%87%22%2C%22%E6%83%85%E6%B7%B1%E6%B7%B1%E9%9B%A8%E8%92%99%E8%92%99%22%2C%22%E5%A4%8F%E5%AE%B6%E4%B8%89%E5%8D%83%E9%87%91%22]; __aryft=1578832977; __aysid=1595774378932ej2; __ayspstp=3; _m_h5_tk=4bb6cb5e6f8c64b63677c6902545838b_1595816682360; _m_h5_tk_enc=98b758a2ad1c2729fe8599154cc133a1; __ayft=1595813073991; __arpvid=1595813142165hd12fb-1595813142199; __ayscnt=1; __aypstp=2; UM_distinctid=1738ddf3bfa136-04be980160c123-15377940-100200-1738ddf3bfbb4; P_ck_ctl=4EA029A51111B84062AED6D1B2B2BB97; modalFrequency={\"UUID\":\"4\",\"times\":1,\"firstTimeExpire\":1595899499478}; ctoken=zKxv6oJyCOgeaopOj1ZMKqcx");
    }

    @Override
    public String getContentId(String filmName) throws Exception {
        return URLEncoder.encode(filmName);
    }

    @Override
    public FilmInfo getFilmInfo(String contentID) throws Exception {
        String url = "https://so.youku.com/search_video/q_" + contentID;
        FilmInfo filmInfo = new FilmInfo();
        String html = getRequest().getHtml(url);
        //System.out.println(html);
        Document doc = Jsoup.parse(html);
        Elements pcart = doc.select("div[class^=pcard");
        if (pcart.size() <= 0) {
            log.error("Canot find pcard :{}" , URLDecoder.decode(contentID));
            return filmInfo;
        }

        Elements move_span = pcart.get(0).select("span[class^=movie-span]");
        if (move_span.size() > 0) {
            if (move_span.text().trim().equals("免费试看")) {
                filmInfo.setNeedVip(true);
            }
        }

        Elements scores = pcart.get(0).select("div[class^=bottom-bg]");
        if (scores.size() > 0) {
            filmInfo.setAverage(Double.parseDouble(scores.text()));
        }

        Elements desc = pcart.get(0).select("div[class^=poster-desc");
        if (desc.size() > 0) {
            String[] infos = desc.get(0).text().split("·");
            if (infos.length < 3) {
                log.error("get poster-desc_RtlVQ error: {} ", URLDecoder.decode(contentID));
                return filmInfo;
            }

            int i = 0;
            for (String info : infos) {
                if (i == 1) {
                    filmInfo.setArriveDate(info.trim());
                }

                if (i==2) {
                    filmInfo.setArriveAddress(info.trim());
                }

                if (i==3) {
                    filmInfo.setPartNum(info.trim());
                }
                i++;
            }
        }



        Elements notce_ = pcart.get(0).select("div[class^=notice_]");

        if (notce_ != null) {
            String last = "";
            String txtNotice = notce_.text().replace("：" , " ");
            String actors[] = txtNotice.split(" ");
            String director = "";
            String actress = "";
            for (String act : actors) {
                if (last.contains("导演") && !act.contains("演员")) {
                    director += act + " ";
                }

                if (last.contains("演员")) {
                    actress += act + " ";
                }

                if (act.contains("导演")) {
                    last = act;
                }

                if (act.contains("演员")) {
                    last = act;
                }
            }

            filmInfo.setDirector(director);
            filmInfo.setActor(actress);
        }



        Elements downUpInfos = pcart.get(0).select("div[class^=down-up-info]");
        for (Element e : downUpInfos) {
//            if (e.text().contains("演员：")) {
//                filmInfo.setActor(e.text().replace("演员：", ""));
//            }

            if (e.text().contains("简介：")) {
                filmInfo.setDesc(e.text().replace("简介：", ""));
            }
        }

        filmInfo.setSuccess(true);
        return filmInfo;
    }
}
